package com.zr.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.InsertUserDao;
import com.zr.model.Canswer;
import com.zr.model.Clog;
import com.zr.model.Cquestion;
import com.zr.model.Crole;
import com.zr.model.Cuser;


@Repository("insertUserDao")
public class InsertUserDaoImpl<T> extends BaseDaoImpl<T> implements InsertUserDao<T>{

	@Override
	public void insertInfoIntoAnswer(String answer,int qid,int uid) {
	
		Canswer can = new Canswer();
		Cquestion cq = new Cquestion();
		cq.setQid(qid);
		Cuser u = new Cuser();
		u.setUid(uid);
		can.setAnswer(answer);
		can.setCquestion(cq);
		can.setCuser(u);
		this.getCurrentSession().save(can);
		
	}

	@Override
	public void insertInfoIntoLog(String time, String uname, int uid, String info) {
		Clog clog = new Clog();
		clog.setLinfo(info);
		clog.setLtime(time);
		clog.setUname(uname);
		clog.setUid(uid);
		this.getCurrentSession().save(clog);
		
	}

	@Override
	public int getRidByRname(String rname) {
		int n = 0;
		String hql = "from Crole where rname=:rname";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("rname", rname);
		List<Crole> cr = que.list();
		Crole r = cr.get(0);
		n=r.getRid();
		return n;
	}

	@Override
	public void insertUserByInfo(String uroot, String upassword, String uname, String utel, int rid, String ustate) {
		Cuser u = new Cuser();
		Crole role = new Crole();
		role.setRid(rid);
		u.setUname(uname);
		u.setUroot(uroot);
		u.setUtel(utel);
		u.setUpassword(upassword);
		u.setUstate(ustate);
		u.setCrole(role);
		this.getCurrentSession().save(u);
	}

	@Override
	public int getUidByInfo(String uroot) {
		int n = 0;
		String hql = "from Cuser where uroot=:uroot";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("uroot", uroot);
		List<Cuser> cr = que.list();
		Cuser u = cr.get(0);
		n=u.getUid();
		return n;
	}

	@Override
	public int getUrootExistsOrNot(String uroot) {
		List<Cuser> us = new ArrayList<Cuser>();
		String hql = "from Cuser where uroot=:uroot";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("uroot", uroot);
		us=que.list();
		return us.size();
	}

	@Override
	public int getQidByQuestion(String question) {
		List<Cquestion> qs = new ArrayList<Cquestion>();
		String hql = "from Cquestion where question=:question";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("question", question);
		qs = que.list();
		return qs.get(0).getQid();
	}
}
